﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace FruitWeigher
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
            GSystem.Root = this;
            GameScore = 0;
            ANI_Zhengque.Completed += new EventHandler(ANI_Zhengque_Completed);
            ANI_Cuowu.Completed += new EventHandler(ANI_Cuowu_Completed);
            GSystem.IsStartShow = true;
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (!GSystem.IsStartShow)
            {
                e.Cancel = true;
                StopGame();
            }
            else
                e.Cancel = false;
            base.OnBackKeyPress(e);
        }
        void ANI_Cuowu_Completed(object sender, EventArgs e)
        {
            GameingLevel -= 1;
            GSystem.UISelected.IsEnabled = true;
        }

        void ANI_Zhengque_Completed(object sender, EventArgs e)
        {
            GameScore += GameingLevel * 5 * GameingLevel;
            GameingLevel += 1;
            GSystem.UISelected.IsEnabled = true;
        }
        private int _GameScore = 0;

        public int GameScore
        {
            get { return _GameScore; }
            set
            {
                _GameScore = value;
                ScoreSum.Text = value.ToString();
            }
        }
        public void StartNewGame()
        {
            GSystem.UISelected.IsEnabled = true;
            GameScore = 0;
            GameMain.Visibility = Visibility.Visible;
            GameingLevel = 1;
            GSystem.UIProgress.MaxSecond = 60;
            GSystem.UIProgress.TimeStart();
        }
        public void StopGame()
        {
            GameStartInterface.Visibility = System.Windows.Visibility.Visible;
            GSystem.UISelected.IsEnabled = false;
            GameScore = 0;
            GameMain.Visibility = Visibility.Collapsed;
            GameingLevel = 1;
            GSystem.UIProgress.MaxSecond = 60;
            GSystem.UIProgress.TimeStop();
            if (GSystem.HighestScore.Visibility == System.Windows.Visibility.Visible)
                GSystem.HighestScore.Close();
            if (GSystem.UIEndMessageBox.Visibility == System.Windows.Visibility.Visible)
                GSystem.UIEndMessageBox.HideThisInterface();
            GameStartInterface.ANI_Over.Begin();
        }
        public int ZhengqueType = 0;
        public void AssertionSelected(int type)
        {
            GSystem.UISelected.IsEnabled = false;
            if (ZhengqueType == type)
                ANI_Zhengque.Begin();
            else
                ANI_Cuowu.Begin();
        }
        private void HideAllItem()
        {
            GameingItem.Clear();
            StackPanel01.Visibility = Visibility.Collapsed;
            StackPanel02.Visibility = Visibility.Collapsed;
            Weigher1.Visibility = Visibility.Collapsed;
            Weigher2.Visibility = Visibility.Collapsed;
            Weigher3.Visibility = Visibility.Collapsed;
            Weigher4.Visibility = Visibility.Collapsed;
        }
        private int _GameingLevel = 1;
        public int GameingLevel
        {
            get { return _GameingLevel; }
            set
            {
                if (value <= 1)
                    _GameingLevel = 1;
                else
                    _GameingLevel = value;
                if (_GameingLevel >= 5)
                    _GameingLevel = 5;
                switch (_GameingLevel)
                {
                    case 1:
                        Level_1();
                        break;
                    case 2:
                        Level_2();
                        break;
                    case 3:
                        Level_3();
                        break;
                    case 4:
                        Level_4();
                        break;
                    case 5:
                        Level_5();
                        break;
                    default:
                        _GameingLevel = 1;
                        Level_1();
                        break;
                }
            }
        }
        public List<ItemInfo> GameingItem = new List<ItemInfo>();
        private void CreateListItemInfo(int max)
        {
            for (int i = 0; i < max; i++)
            {
                ItemInfo s;
                bool r = false;
                do
                {
                    r = false;
                    s.Type = GSystem.GamesRandom.Next() % 5 + 1;
                    foreach (var item in GameingItem)
                    {
                        if (s.Type == item.Type)
                            r = true;


                    }
                } while (r);
                s.Heft = i + 1;
                GameingItem.Add(s);
            }
        }
        private void overCreate()
        {
            ZhengqueType = GameingItem[GameingItem.Count - 1].Type;

            for (int i = 0; i < GameingItem.Count; i++)
            {
                ItemInfo s = GameingItem[i];
                GameingItem.Remove(s);
                GameingItem.Insert(GSystem.GamesRandom.Next() % GameingItem.Count, s);
            }

            GSystem.UISelected.UpDataCardList(GameingItem);
        }
        public void Level_1()
        {
            HideAllItem();
            StackPanel01.Visibility = Visibility.Visible;
            Weigher1.Visibility = Visibility.Visible;

            CreateListItemInfo(2);



            if (GSystem.GamesRandom.Next() % 2 == 1)
                Weigher1.CreateItemShow(GameingItem[0], null, GameingItem[1], null);
            else
                Weigher1.CreateItemShow(GameingItem[1], null, GameingItem[0], null);
            overCreate();
        }
        public void Level_2()
        {
            HideAllItem();
            StackPanel01.Visibility = Visibility.Visible;
            Weigher1.Visibility = Visibility.Visible;
            Weigher2.Visibility = Visibility.Visible;
            CreateListItemInfo(3);

            switch (GSystem.GamesRandom.Next() % 3)
            {
                case 0:
                    Weigher1.CreateItemShow(GameingItem[0], null, GameingItem[1], null);
                    Weigher2.CreateItemShow(GameingItem[1], null, GameingItem[2], null);
                    break;
                case 1:
                    Weigher1.CreateItemShow(GameingItem[2], null, GameingItem[1], null);
                    Weigher2.CreateItemShow(GameingItem[1], null, GameingItem[0], null);
                    break;
                case 2:
                    Weigher1.CreateItemShow(GameingItem[0], null, GameingItem[1], null);
                    Weigher2.CreateItemShow(GameingItem[2], null, GameingItem[1], null);
                    break;
            }



            overCreate();
            /*if(GSystem.GamesRandom.Next() % 2 == 1)
                Weigher2.CreateItemShow(GameingItem[2], null, GameingItem[1], GameingItem[0]);
            else
                Weigher2.CreateItemShow(GameingItem[0], GameingItem[1],null , GameingItem[2]);*/
        }
        public void Level_3()
        {
            HideAllItem();
            StackPanel01.Visibility = Visibility.Visible;
            StackPanel02.Visibility = Visibility.Visible;
            Weigher1.Visibility = Visibility.Visible;
            Weigher2.Visibility = Visibility.Visible;
            Weigher3.Visibility = Visibility.Visible;
            CreateListItemInfo(3);

            switch (GSystem.GamesRandom.Next() % 3)
            {
                case 0:
                    Weigher1.CreateItemShow(GameingItem[2], null, GameingItem[1], GameingItem[0]);
                    Weigher2.CreateItemShow(GameingItem[0], null, GameingItem[1], null);
                    Weigher3.CreateItemShow(GameingItem[1], null, GameingItem[2], null);
                    break;
                case 1:
                    Weigher3.CreateItemShow(GameingItem[0], null, GameingItem[1], GameingItem[2]);
                    Weigher2.CreateItemShow(GameingItem[1], null, GameingItem[0], null);
                    Weigher1.CreateItemShow(GameingItem[2], null, GameingItem[1], null);
                    break;
                case 2:
                    Weigher1.CreateItemShow(GameingItem[1], GameingItem[0], null, GameingItem[2]);
                    Weigher3.CreateItemShow(GameingItem[2], GameingItem[0], GameingItem[1], null);
                    Weigher2.CreateItemShow(GameingItem[1], null, GameingItem[0], null);
                    break;
            }
            overCreate();
        }
        public void Level_4()
        {
            HideAllItem();
            StackPanel01.Visibility = Visibility.Visible;
            StackPanel02.Visibility = Visibility.Visible;
            Weigher1.Visibility = Visibility.Visible;
            Weigher2.Visibility = Visibility.Visible;
            Weigher3.Visibility = Visibility.Visible;
            Weigher4.Visibility = Visibility.Visible;
            CreateListItemInfo(4);

            switch (GSystem.GamesRandom.Next() % 3)
            {
                case 0:
                    Weigher1.CreateItemShow(GameingItem[0], GameingItem[3], GameingItem[1], GameingItem[2]);
                    Weigher2.CreateItemShow(GameingItem[1], null, GameingItem[3], null);
                    Weigher3.CreateItemShow(GameingItem[2], null, GameingItem[0], null);
                    Weigher4.CreateItemShow(null, GameingItem[2], null, GameingItem[3]);
                    break;
                case 1:
                    Weigher4.CreateItemShow(GameingItem[0], GameingItem[3], GameingItem[1], GameingItem[2]);
                    Weigher3.CreateItemShow(GameingItem[1], null, GameingItem[3], null);
                    Weigher2.CreateItemShow(GameingItem[2], null, GameingItem[0], null);
                    Weigher1.CreateItemShow(null, GameingItem[2], null, GameingItem[3]);
                    break;
                case 2:
                    Weigher1.CreateItemShow(GameingItem[0], GameingItem[1], GameingItem[2], null);
                    Weigher2.CreateItemShow(GameingItem[0], GameingItem[1], GameingItem[3], null);
                    Weigher3.CreateItemShow(GameingItem[3], GameingItem[1], GameingItem[2], null);
                    Weigher4.CreateItemShow(null, GameingItem[2], GameingItem[1], GameingItem[3]);
                    break;
            }
            overCreate();
        }
        public void Level_5()
        {
            HideAllItem();
            StackPanel01.Visibility = Visibility.Visible;
            StackPanel02.Visibility = Visibility.Visible;
            Weigher1.Visibility = Visibility.Visible;
            Weigher2.Visibility = Visibility.Visible;
            Weigher3.Visibility = Visibility.Visible;
            Weigher4.Visibility = Visibility.Visible;
            CreateListItemInfo(5);

            if (GSystem.GamesRandom.Next() % 2 == 1)
            {
                Weigher1.CreateItemShow(GameingItem[0], null, GameingItem[1], null);
                Weigher2.CreateItemShow(GameingItem[1], GameingItem[2], GameingItem[3], null);
                Weigher3.CreateItemShow(GameingItem[2], null, GameingItem[4], null);
                Weigher4.CreateItemShow(GameingItem[3], GameingItem[4], GameingItem[1], null);
            }
            else
            {
                Weigher4.CreateItemShow(GameingItem[0], null, GameingItem[1], null);
                Weigher3.CreateItemShow(GameingItem[1], GameingItem[2], GameingItem[3], null);
                Weigher2.CreateItemShow(GameingItem[2], null, GameingItem[4], null);
                Weigher1.CreateItemShow(GameingItem[3], GameingItem[4], GameingItem[1], null);
            }
            overCreate();
        }
    }
}